package net.geektao.lab.spring.beansoftype;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 业务处理器 Manager，提供自动注册业务处理器功能。
 * 
 * @author geektao
 *
 */
public class BusinessProcessorsManager implements InitializingBean, ApplicationContextAware {

	private Logger logger = Logger.getLogger(this.getClass().getName());
	private ApplicationContext appCtx;

	private List<BusinessProcessor> processors = new ArrayList<>();

	@Override
	public void afterPropertiesSet() throws Exception {
		Map<String, BusinessProcessor> map = appCtx.getBeansOfType(BusinessProcessor.class);

		logger.log(Level.INFO, "开始注册BusinessProcessor");
		for (String beanName : map.keySet()) {
			processors.add(map.get(beanName));
			logger.log(Level.INFO, "注册BusinessProcessor：" + beanName);
		}
		logger.log(Level.INFO, "注册BusinessProcessor完成。");
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.appCtx = applicationContext;
	}

}
